package org.colcon.web.server;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.colcon.web.client.ColConService;
import org.colcon.web.model.Ballot;
import org.colcon.web.model.CLI;
import org.colcon.web.model.Group;
import org.colcon.web.model.Item;
import org.colcon.web.model.Model;
import org.colcon.web.model.User;
import org.colcon.web.model.framework.IDecision;
import org.colcon.web.model.framework.IGroup;
import org.colcon.web.model.framework.IProposal;
import org.colcon.web.model.framework.ITitle;
import org.colcon.web.model.framework.IUser;
import org.colcon.web.model.framework.IVote;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class ColConServiceImpl 
	extends RemoteServiceServlet 
	implements ColConService 
{
	private static final long serialVersionUID = 5492849777369331376L;

	public ColConServiceImpl()
	{
		Model.init();
	}
	
	public String execute(String cmd)
	{
		return CLI.execute(cmd);
	}
	
	public Item get(String path)
	{
		return (Item) Model.get(path);
	}
	
	public List<Group> getGroups()
	{
		List<Group> list = new ArrayList<Group>();
		
		for(IGroup group : Model.getGroups())
			list.add((Group) group);
		
		return list;
	}
	
	public Boolean addGroup(String id, String charter)
	{
		return Model.addGroup(new Group(id, charter));
	}
	
	public User getUser(String id)
	{
		return (User) Model.getUser(id);
	}
	
	public List<User> getUsers()
	{
		List<User> list = new ArrayList<User>();
		
		for(IUser user : Model.getUsers())
			list.add((User) user);
		
		return list;
	}
	
	public Boolean addUser(String id, String email, String password)
	{
		return Model.addUser(new User(id, email, password));
	}
	
	public User authenticate(String id, String password)
	{
		User user = (User) Model.getUser(id);
		
		if(user == null || !user.authenticate(password))
			return null;
		
		return user;
	}
	
	public Group getGroup(String id)
	{
		return (Group) Model.getGroup(id);
	}
	
	public Boolean addMember(String groupname, String userId)
	{
		IGroup group = Model.getGroup(groupname);
		
		if(group == null)
			return false;
		
		return group.addMember(userId);
	}
	
	public Boolean usePower(String titlePath, String power, Map<String, String> params)
	{
		ITitle title = (ITitle) Model.get(titlePath);
		
		if(title == null)
			return false;
		
		IDecision decision = title.getPower(power).getDecision(params);
		CLI.execute(decision.getCmd());
		
		return true;
	}
	
	public Ballot submitVote(String votePath, String userId, String voteValue)
	{
		IVote vote = (IVote) Model.get(votePath);
		IUser user = Model.getUser(userId);
		
		return (Ballot) vote.vote(user, voteValue);
	}
	
	public Ballot submitProposal(String votePath, String userId, String voteValue)
	{
		IProposal proposal = (IProposal) Model.get(votePath);
		IUser user = Model.getUser(userId);
		
		return (Ballot) proposal.vote(user, voteValue);
	}
}
